<?php



namespace plugins\goods\interfaceLib;   



use think\Db;



use cmf\controller\PluginAdminBaseController;//引用插件基类

use plugins\freight\model\GoodsFreightModel;

use plugins\store\Common;





/**



 * 分类控制类



 */



class GoodsFreight extends PluginAdminBaseController

{

    /**

     * 初始化

     */

    private  $com = '';

    protected function _initialize()

    {

        parent::_initialize();

        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录

        //dump($adminId);exit;

        if (!empty($adminId)) {

            $this->assign("admin_id", $adminId);

        }

        $this->com = new Common;

        

    }

    







    /**

     * 店铺运费模板选项

     */



    public function storeFreight($name,$freight_id)

    {



        //用户权限判断

        $allow = $this->com->getAllowAccess();

        $where ="( store_id='".$allow."' or id = 1 )";

        if($allow == 'true') $where = 1;

        $freight = db('freight')->where($where)->field('id,title')->select();



        if( !empty($freight_id) ){

            $this->assign('freight_id', $freight_id);

        } 



        $this->assign( 'freight' , $freight );

        $this->assign( 'name' , $name );



        return $this->fetch('freight_plugin/freightControl');



    } 



    



       



    /**

     * 查询运费价格

     * 

     * @param  $freight_id 运费id

     * @param  $province   市

     * @param  $goods_num  购买数量

     * @param  $store_id   店铺id

     * @return \返回json字符串

     */

    public function getFreight($freight_id = '',$province='',$goods_num=0,$store_id=0){



        if(empty($freight_id) || empty($province) || $goods_num<=0 ){

            return zy_array (false,'参数异常','',100 ,false);

        }

        // $allow = $this->com->getAllowAccess();//店铺id

        // if($allow !==true) {

        //     $store_id=$allow;

        // }

        $freight_price = 0;//最后运费

        $id = $freight_id;//运费id

        //购买数量

        //获取运费模板的详情

        $freight = Db::name('freight')->where(['id'=>$id,'store_id'=>$store_id])->find();

        if(!$freight) return zy_array(false,'未找到记录','',101,false);

        

        //查询运费模板详情是否有匹配的省

        $list = Db::name('freight_item')->where(['freight_id'=>$id])->select();

        $GFM = new GoodsFreightModel();

        $freight_price = $GFM->getPrice($freight,$goods_num,$freight_price);//默认运费计算

        

        //特殊运费计算

        if(sizeof($list)>0){

            foreach ($list as $freight_item){

                if(strpos($freight_item['area'],$province) !== false ){//范围内

                    $cut_price = $goods_num-$freight_item['first_num'];//超首件数

                    if( $cut_price < $freight_item['continue_num'] ){//首超小于续件

                        $freight_price = $freight_item['first_price'];//首价

                    }else{

                        $freight_prices = $freight_item['continue_price'] * floor($cut_price/$freight_item['continue_num']) + $freight_item['first_price'];//运费= 续费*(超首件数/续件数)+首费

                        if($freight_prices>$freight_price) $freight_price = $freight_prices;

                    }

                }

            }

        }

        $res = array(

            'freight_price'=>$freight_price

        );

        return zy_array(true,'查询成功',$res,200,$isModule);

    }





}